package com._520it.crm.web.controller;

import com._520it.crm.domain.Check;
import com._520it.crm.domain.OrderBill;
import com._520it.crm.page.AjaxResult;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.CheckQueryObject;
import com._520it.crm.service.ICheckService;
import com._520it.crm.service.IOrderBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/check")
public class CheckController {

    @Autowired
    private ICheckService checkService;
    @Autowired
    private IOrderBillService orderBillService;

    @RequestMapping("")
    public String index() {
        return "check";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(CheckQueryObject qo) {
        PageResult result = checkService.queryPage(qo);
        return result;
    }

    @RequestMapping("/update")
    @ResponseBody
    public AjaxResult update(Check check){
        AjaxResult result = null;
        OrderBill orderBill = null;
        try{
            orderBill = new OrderBill();
            orderBill.setId(check.getOrderBill().getId());
            orderBill.setStatus(5);
            checkService.updateByPrimaryKey(check);
            orderBillService.updateStatusByPay(orderBill);
            result = new AjaxResult(true,"缴费成功");
        }catch(Exception e){
            e.printStackTrace();
            result = new AjaxResult("缴费失败,请联系管理员!");
        }
        return result;
    }

    @RequestMapping("/slotCardUpdate")
    @ResponseBody
    public AjaxResult slotCardUpdate(Check check){
        AjaxResult result = null;
        try{
            checkService.slotCardUpdate(check);
            result = new AjaxResult(true,"缴费类型转换成功");
        }catch(Exception e){
            e.printStackTrace();
            result = new AjaxResult("缴费类型转换失败,请联系管理员!");
        }
        return result;
    }

    @RequestMapping("/cheque")
    @ResponseBody
    public AjaxResult cheque(Check check){
        AjaxResult result = null;
        try{
            checkService.cheque(check);
            result = new AjaxResult(true,"缴费类型转换成功");
        }catch(Exception e){
            e.printStackTrace();
            result = new AjaxResult("缴费类型转换失败,请联系管理员!");
        }
        return result;
    }

    @RequestMapping("/issueSendBack")
    @ResponseBody
    public AjaxResult issueSendBack(Long id, Long orderBillId){
        AjaxResult result = null;
        try{
            checkService.issueSendBack(id);
            orderBillService.issueSendBackUpdate(orderBillId);
            result = new AjaxResult(true,"缴费类型转换成功");
        }catch(Exception e){
            e.printStackTrace();
            result = new AjaxResult("缴费类型转换失败,请联系管理员!");
        }
        return result;
    }

}
